package com.mediacloud.app.newsmodule.activity.military_encyclopedia

import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mediacloud.app.assembly.util.GlidePlaceholderDrawable
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.assembly.util.otherLoad
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.newsmodule.R

class MilitaryEncyclopediaTopicTopAdapter : BaseQuickAdapter<ArticleItem,BaseViewHolder>(R.layout.item_bk_topic_top) {
    var articleId :Long = 0
    override fun convert(helper: BaseViewHolder, item:ArticleItem) {
        val rightLine = helper.getView<View>(R.id.rightLine)
        val leftLine = helper.getView<View>(R.id.leftLine)
        val viewCicle = helper.getView<View>(R.id.viewCicle)
        val tvTopicTitle = helper.getView<TextView>(R.id.tvTopicTitle)
        val ivLogo = helper.getView<PolygonImageView>(R.id.ivLogo)

        otherLoad(ivLogo.context,item.logo,object :RequestListener<Drawable?>{
            override fun onLoadFailed(
                e: GlideException?,
                model: Any?,
                target: Target<Drawable?>?,
                isFirstResource: Boolean
            ): Boolean {
                ivLogo.setImageDrawable(GlidePlaceholderDrawable(ivLogo.resources, com.mediacloud.app.reslib.R.drawable.cctv7_loading_img))
                return true
            }

            override fun onResourceReady(
                resource: Drawable?,
                model: Any?,
                target: Target<Drawable?>?,
                dataSource: DataSource?,
                isFirstResource: Boolean
            ): Boolean {
                ivLogo.setImageDrawable(resource)
                return true
            }}
            )

        if (helper.absoluteAdapterPosition == 0){
            leftLine.visibility = View.INVISIBLE
        }else{
            leftLine.visibility = View.VISIBLE
        }
        if (helper.absoluteAdapterPosition ==  itemCount-1){
            rightLine.visibility = View.INVISIBLE
        }else{
            rightLine.visibility = View.VISIBLE
        }
        if (item.id == articleId){
            viewCicle.background = ContextCompat.getDrawable(viewCicle.context,R.drawable.icon_bk_select)
            ivLogo.setBorderColor(ivLogo.context.getColor(R.color.cctv7_red))
        }else{
            viewCicle.background = ContextCompat.getDrawable(viewCicle.context,R.drawable.icon_bk_unselect)
            ivLogo.setBorderColor(ivLogo.context.getColor(R.color.c_e8e8e8))
        }
        tvTopicTitle.text = item.title

    }

}